﻿var file_list = [];
var videoObjects = [];
var isInstallUploadingPlugin = true;
; (function() {
    var ue;
    var uploadVideoList = [];    

    window.onload = function() {
        ue = window.parent.ue;
        initTabs();
        initVideo();
    };

    function initTabs() {
        var tabs = $G('tabHeads').children;
        for (var i = 0; i < tabs.length; i++) {
            domUtils.on(tabs[i], "click", function(e) {
                var j, bodyId, target = e.target || e.srcElement;
                for (j = 0; j < tabs.length; j++) {
                    bodyId = tabs[j].getAttribute('data-content-id');
                    if (tabs[j] == target) {
                        domUtils.addClass(tabs[j], 'focus');
                        domUtils.addClass($G(bodyId), 'focus');
                    } else {
                        domUtils.removeClasses(tabs[j], 'focus');
                        domUtils.removeClasses($G(bodyId), 'focus');
                    }
                }
            });
        }

        tabs[1].click();
    }

    /**
    * 监听url改变事件
    * @param url
    */
    function addUrlChangeListener(url) {
        if (browser.ie) {
            url.onpropertychange = function() {
                createPreviewVideo(this.value);
            }
        } else {
            url.addEventListener("input", function() {
                createPreviewVideo(this.value);
            }, false);
        }
    }

    /**
    * 根据url生成视频预览
    * @param url
    */
    function createPreviewVideo(url) {
        if (!url) return;
        setTimeout(function() {
            var conUrl = convert_url(url);
            var $preview = document.getElementById('preview');
            //        var AcxObj = document.createElement("OBJECT");
            //        AcxObj.setAttribute("classid", "clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921");
            //        AcxObj.setAttribute("height", "280");
            //        AcxObj.setAttribute("width", "420");
            //        AcxObj
            //        $preview.appendChild(AcxObj);
            $preview.innerHTML = '';
            $preview.innerHTML = '<object id="player" classid=\"clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921\" class="previewVideo" codebase=\"http://downloads.videolan.org/pub/videolan/vlc/last/win32/vlc-2.2.5.1-win32.exe\"' +
                ' events=\"True\">' +
                "<param name=\"Src\" value=\"" + conUrl +
                "\" /><param name=\"ShowDisplay\" value=\"True\" />" +
                "<param name=\"AutoLoop\" value=\"False\" /><param name=\"AutoPlay\" value=\"true\" />" +
                '<embed type="application/x-vlc-plugin" pluginspage="http://www.videolan.org"' +
                ' src="' + conUrl + '" ' +
                'width="420" height="280" wmode="transparent" play="true" loop="false" menu="false" allowfullscreen="true">' +
                '</embed>' +
                '</object>';

            var player = document.getElementById('player');
            player.setAttribute("width", "420");
            player.setAttribute("height", "280");
            player.style.width = "420px";
            player.style.height = "280px";
            player.getElementsByTagName("param")[0].value = conUrl;

        }, 1000);
    }

    function convert_url(url) {
        if (!url) return '';
        url = utils.trim(url)
            .replace(/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i, 'player.youku.com/player.php/sid/$1/v.swf')
            .replace(/(www\.)?youtube\.com\/watch\?v=([\w\-]+)/i, "www.youtube.com/v/$2")
            .replace(/youtu.be\/(\w+)$/i, "www.youtube.com/v/$1")
            .replace(/v\.ku6\.com\/.+\/([\w\.]+)\.html.*$/i, "player.ku6.com/refer/$1/v.swf")
            .replace(/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "player.56.com/v_$1.swf")
            .replace(/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "player.56.com/v_$1.swf")
            .replace(/v\.pps\.tv\/play_([\w]+)\.html.*$/i, "player.pps.tv/player/sid/$1/v.swf")
            .replace(/www\.letv\.com\/ptv\/vplay\/([\d]+)\.html.*$/i, "i7.imgs.letv.com/player/swfPlayer.swf?id=$1&autoplay=0")
            .replace(/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i, "www.tudou.com/v/$1")
            .replace(/v\.qq\.com\/cover\/[\w]+\/[\w]+\/([\w]+)\.html/i, "static.video.qq.com/TPout.swf?vid=$1")
            .replace(/v\.qq\.com\/.+[\?\&]vid=([^&]+).*$/i, "static.video.qq.com/TPout.swf?vid=$1")
            .replace(/my\.tv\.sohu\.com\/[\w]+\/[\d]+\/([\d]+)\.shtml.*$/i, "share.vrs.sohu.com/my/v.swf&id=$1");

        return url;
    }

    function initVideo() {
        var container = $('#filePickerReady');
        container.addClass("webuploader-container");
        var allowfilestr = '';
        var fileSize = editor.options.videoFileSizeLimit || editor.getOpt('videoMaxSize') || 104857600; //如果配置里面没有，就默认100M
        var fileSizeLimit = (fileSize / (1024 * 1024)).toString().split('.')[0];
        if (fileSizeLimit == 0) {
            fileSizeLimit = (fileSize / (1024 * 1024)).toString().split('.')[0] + '.' + (fileSize / (1024 * 1024)).toString().split('.')[1].substring(0, 2);
        }
        var allowFiles = editor.options.videoFileTypes || editor.getOpt('videoAllowFiles');
        for (var i = allowFiles.length - 1; i >= 0; i--) {
            if (i === allowFiles.length - 1) {
                allowfilestr += allowFiles[i];
            } else {
                allowfilestr += allowFiles[i] + '、';
            }
        }
        var str = '<div class="webuploader-pick">点击选择文件</div>' +
            '<div id="rt_rt_1bgn2vp7ppeo1re78ea49k10t1" style="position: absolute; top: 0px; left: 123.094px; width: 168px; height: 44px; overflow: hidden; bottom: auto; right: auto;">' +
            '<input id="fileInput" type="file" name="file" class="webuploader-element-invisible" multiple="multiple" onchange="addFile(this);">' +
            '<label style="opacity: 0;filter:Alpha(opacity: 0); width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);" onclick="labelClick();"></label></div>' +
            '<input id="objForGetFileName" style="z-index: -1;opacity: 0;filter:Alpha(opacity: 0);" type="radio" class="diyHide">' +
            '<p style="font-size:13px;word-wrap:break-word; word-break:break-all; ">请上传不大于' + fileSizeLimit + 'MB的' + allowFiles + '格式的文件</p>';

        container.append(str);
        var $preview = document.getElementById('preview');
        $preview.innerHTML = '<object id="player" classid=\"clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921\" class="previewVideo" codebase=\"http://downloads.videolan.org/pub/videolan/vlc/last/win32/vlc-2.2.5.1-win32.exe\"' +
            ' events=\"True\">' +
            "<param name=\"Src\" value=\"" + '' +
            "\" /><param name=\"ShowDisplay\" value=\"True\" />" +
            "<param name=\"AutoLoop\" value=\"False\" /><param name=\"AutoPlay\" value=\"false\" />" +
            '</object>';
        var player = document.getElementById('player');
        player.setAttribute("width", "420");
        player.setAttribute("height", "280");
        player.style.width = "420px";
        player.style.height = "280px";
        addUrlChangeListener($G("videoUrl"));
        addOkListener();
    }

})()

function labelClick() {
    $('#fileInput').trigger('click');
}

/**
 *  非H5用ActiveX插件获取文件大小
 */
function getLocalFileSize(filePath) {
    var fileSize;
    try {
        var axObj = new ActiveXObject("LgJCPT.ClsLgJCPT");
        fileSize = axObj.GetLocalFileLen(filePath);
    } catch (e) {
        if (isInstallUploadingPlugin) {
            alert("未安装LgJCPT.ClsLgJCPT插件!请到登陆页下载基础插件包。");
            isInstallUploadingPlugin = false;
        }
        return -1;
    }

    return fileSize;  
}

/*
* 更改文件显示单位
*/
function changeSize(fileSize) {
    var units = ["B", "KB", "MB", "GB", "TB"];
    var index = 0;
    if (fileSize < 1024) {
        return fileSize + "B";
    } else {
        while (fileSize >= 1024) {
            fileSize = fileSize / 1024;
            index += 1;
        }
        return fileSize.toFixed(2) + units[index];
    }
}

function addFile(obj) {
    var sizeLimit = editor.options.videoFileSizeLimit || editor.getOpt('videoMaxSize');
    if (getLocalFileSize(getFileName(obj)) > sizeLimit) {
        /*var fileSizeLimit = '';
        if (fileSizeLimit == 0) {
            fileSizeLimit = (sizeLimit / (1024 * 1024)).toString().split('.')[0] + '.' + (sizeLimit / (1024 * 1024)).toString().split('.')[1].substring(0, 2);
        }*/
        alert('文件大小超出' + changeSize(sizeLimit) + '限制');
        return;

    } else if (getLocalFileSize(getFileName(obj)) == -1) {
        isInstallUploadingPlugin = true;
        return;
    }
    file_list.push(getFileName(obj));
    var str = '<div id="upload" class="panel focus">' +
        '<div id="queueList" class="queueList">' +
        '<div class="statusBar">' +
        '<div class="progress" style="display: none;">' +
        '<div class="file-panel" style="height: 0px;"><span class="cancel">删除</span></div>' +
        '<span class="text">0%</span>' +
        '<span class="percentage" style="width: 0%;"></span>' +
        '</div><div class="info" style="display: inline-block;">选中' + file_list.length +'个文件，共' + getTotalFileSize() + 'M。</div>' +
        '<div class="btns">' +
        '<div id="filePickerBtn" class="webuploader-container">' +
        '<input type="hidden" id="hiddenFiles" name="hiddencolor" value="" />' +
        '<div class="webuploader-pick">继续添加</div>' +
        '<div id="rt_rt_1bgdnai8p1re9cbse15e818qc6" style="position: absolute; top: 0px; left: 10px; width: 94px; height: 32px; overflow: hidden; bottom: auto; right: auto;">' +
        '<input id="fileInput2" onchange="addItem(this);" type="file" name="file" class="webuploader-element-invisible" multiple="multiple">' +
        '<label id="addFile" style="opacity: 0;filter:Alpha(opacity: 0); width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);"></label></div></div>' +
        '<input id="objForGetFileName" style="z-index: -1;opacity: 0;filter:Alpha(opacity: 0);" type="radio" class="diyHide">' +
        '<div class="uploadBtn state-ready" onclick="startUpload();">开始上传</div>' +
        '</div>' +
        '</div>' +
        '<div id="dndArea" class="placeholder element-invisible">' +
        '<div class="filePickerContainer">' +
        '<div id="filePickerReady" class="webuploader-container"><div class="webuploader-pick">点击选择文件</div><div id="rt_rt_1bgdnai8m17kg6meo6mtu1uot1" style="position: absolute; top: 0px; left: 0px; width: 78px; height: 264px; overflow: hidden; bottom: auto; right: auto;"><input type="file" name="file" class="webuploader-element-invisible" multiple="multiple"><label style="opacity: 0; width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);"></label></div></div>' +
        '</div>' +
        '</div>' +
        '<ul class="filelist">' +
        '<li id="WU_FILE_' + file_list.length + '"><p class="title">' +
        getExactFileName(getFileName(obj)) + '</p><p class="imgWrap notimage"><i class="file-preview file-type-mov"></i><span class="file-title">' +
        getExactFileName(getFileName(obj)) + '</span></p><p class="progress"><span></span></p><div class="file-panel" style="height: 0px;"><span class="cancel">删除</span><span class="rotateRight">向右旋转</span><span class="rotateLeft">向左旋转</span></div><p class="error" style="display: none;"></p>' +
        //'<li id="WU_FILE_' + file_list.length + '"><p class="title">' +
        //getExactFileName(getFileName(obj)) + '</p><p class="progress"><span style="display: block; width: 0;"></span></p><div class="file-panel" style="height: 0px;"><span class="cancel">删除</span><span class="rotateRight">向右旋转</span><span class="rotateLeft">向左旋转</span></div>' +
        '<p class="error" style="' +
        (getIsLegal(getFileName(obj)) ? 'display: none;' : 'display: block;') +
        '">文件格式不允许</p><span class="success" style="display:none;"></span></li><li id="filePickerBlock" class="filePickerBlock webuploader-container"><div class="webuploader-pick"></div><div id="rt_rt_1bgdnai8o1a341jsa1esf19s41ufp4" style="position: absolute; top: 0px; left: 0px; width: 113px; height: 113px; overflow: hidden; bottom: auto; right: auto;"><input onchange="addItem(this);" type="file" name="file" class="webuploader-element-invisible" multiple="multiple">' +
        '<label id="addFileLabel" style="opacity: 0; filter:Alpha(opacity: 0);width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);"></label></div></li>' +
        '</ul>' +
        '</div>' +
        '</div>';
    if (file_list.length === 1) {
        $('#upload_left').html(str);
    }


    $(document).on('mouseenter', '.filelist li', function () {
        $(this).find('.file-panel').stop().animate({ height: 30 });
    });
    $(document).on('mouseleave', '.filelist li', function () {
        $(this).find('.file-panel').stop().animate({ height: 0 });
    });

    $(document).on('click', '.file-panel span', function () {
        file_list.splice($(this).closest('li').index(), 1);
        $(this).closest('li').remove();
        removeFile();
    });

    $('#addFile').on('click', function () {
        $('#fileInput2').trigger('click');
    });

    $('#addFileLabel').on('click', function () {
        $('#fileInput2').trigger('click');
    });
}

function removeFile() {
    var $ul = $('.filelist');
    var i = 1;
    $ul.find('li').each(function (item) {
        //var key=$(this).text()
        if($(this)[0].id != 'filePickerBlock') {
           $(this)[0].id = 'WU_FILE_' + i;
           
           i += 1;
        }
    });
    $('.info').text('选中' + file_list.length + '个文件，共' + getTotalFileSize() + 'MB。');
}

//兼容IE低版本获取上传文件名
function getFileName(obj) {
    var filename = "";
    try {
        obj.select();
        document.getElementById("objForGetFileName").focus();
        filename = document.selection.createRange().text;
    } catch (e) { }
    finally {
        document.selection.empty();
    }
    return filename;
};

function getIsLegal(path) {
    var tmp = path.split('.');
    var ext = '.' + tmp[tmp.length - 1].toLowerCase();
    var allowFiles = editor.options.videoFileTypes || editor.getOpt('videoAllowFiles');
    /*var videoAllowFiles = [
        "flv", "swf", "mkv", "avi", "rm", "rmvb", "mpeg", "mpg",
        "ogg", "ogv", "mov", "wmv", "mp4", "webm", "wav", "mid"];*/
    for (var i = 0; i < allowFiles.length; i++) {
        if (allowFiles[i] == ext) {
            return true;
        }
    }

    return false;
}

function addItem(obj) {
    var sizeLimit = editor.options.videoFileSizeLimit || editor.getOpt('videoMaxSize');
    if (getLocalFileSize(getFileName(obj)) > sizeLimit) {
        alert('文件大小超出' + changeSize(sizeLimit) + '限制');
        return;
    }
    var filename = getFileName(obj);
    file_list.push(filename);
    var str = '<li id="WU_FILE_' + file_list.length + '">' +
        '<p class="title">' +
        getExactFileName(filename) +
        '</p>' +
        '<p class="imgWrap notimage">' +
        '<i class="file-preview file-type-mov"></i><span class="file-title"></span>' +
        '</p>' +
        '<p class="progress">' +
        '<span  style="display: block; width: 0;"></span>' +
        '</p>' +
        '<div class="file-panel" style="height: 0px;">' +
        '<span class="cancel">删除</span><span class="rotateRight">向右旋转</span><span class="rotateLeft">向左旋转</span>' +
        '</div><p class="error" style="' +
        (getIsLegal(getFileName(obj)) ? 'display: none;' : 'display: block;') +
        '">文件格式不允许</p><span class="success" style="display: none;"/>' +
        '</li>';
    $('.info').text('选中' + file_list.length + '个文件，共' + getTotalFileSize() + 'MB。');
    if (file_list.length === 1) {
        $('#filePickerBlock').before(str);
    } else {
        $('#WU_FILE_' + (file_list.length - 1)).after(str);
    }
    //$('#WU_FILE_' + (file_list.length - 1)).after(str);
}

function getExactFileName(str) {
    var arr = str.split('\\');

    return arr[arr.length - 1];
}

function startUpload() {
    for (var i = 0; i < file_list.length; i++) {
        if (getIsLegal(file_list[i])) {
            doUpload(i);
        }
    }
}

function getUid(prefix) {
    var counter = 0;
    var guid = (+new Date()).toString(32),
        i = 0;

    for (; i < 5; i++) {
        guid += Math.floor(Math.random() * 65535).toString(32);
    }

    return (prefix || 'wu_') + guid + (counter++).toString(32);
}

function initUploadOcx() {
    var upload_ocx;
    var address = getRootPath_web();
    //首次初始化
    var isExists = false;
    try {
        upload_ocx = new ActiveXObject("CSUpload.ctrlUpload");
        isExists = true;
    } catch (e) {
        upload_ocx_initErrorMsg = "上传插件CSUpload未安装!";
        upload_ocx_initFlag = false;
        alert(upload_ocx_initErrorMsg);
    }

    if (isExists) {
        //初始化ocx参数
//        var location = document.scripts;
//            location = location[location.length - 1].src.substring(0, location[location.length - 1].src.lastIndexOf("/") + 1);
        var address = editor.getOpt('ftpServerInfo');
        try {
            if (!upload_ocx.InitServerInfo(address + 'UEditorUploadHandler.ashx', 2, 'Videos', 'UEditor')) {
                upload_ocx_initErrorMsg = "ActiveX插件初始化失败!";
                upload_ocx_initFlag = false;
                alert(upload_ocx_initErrorMsg);
            }
        } catch (e) {
            upload_ocx_initErrorMsg = "ActiveX插件初始化失败!";
            upload_ocx_initFlag = false;
            alert(upload_ocx_initErrorMsg);
        }
    }

    return upload_ocx;
}

 /**
  * 监听确认和取消两个按钮事件，用户执行插入或者清空正在播放的视频实例操作
  */
function addOkListener() {
    dialog.onok = function () {
        $G("preview").innerHTML = "";
        
        var currentTab = findFocus("tabHeads", "tabSrc");
        switch (currentTab) {
            case "video":
                return insertSingle();
                break;
            case "videoSearch":
                return insertSearch("searchList");
                break;
            case "upload":
                return insertUpload();
                break;
        }
    };
    dialog.oncancel = function () {
        $G("preview").innerHTML = "";
    };
}
 /**
    * 找到id下具有focus类的节点并返回该节点下的某个属性
    * @param id
    * @param returnProperty
    */
    function findFocus(id, returnProperty) {
        var tabs = $G(id).children,
                property;
        for (var i = 0, ci; ci = tabs[i++]; ) {
            if (ci.className == "focus") {
                property = ci.getAttribute(returnProperty);
                break;
            }
        }
        return property;
    }
    
    /**
    * 将单个视频信息插入编辑器中
    */
    function insertSingle() {
        var width = $G("videoWidth"),
            height = $G("videoHeight"),
            url = $G('videoUrl').value,
            align = findFocus("videoFloat", "name");
        if (!url) return false;
        if (!checkNum([width, height])) return false;
        var ue = window.parent.ue;
        ue.execCommand('insertvideo', {
            url: convert_url(url),
            width: width.value,
            height: height.value,
            align: align
        }, 'upload');
    }
    
    /**
    * 数字判断
    * @param value
    */
    function isNumber(value) {
        return /(0|^[1-9]\d*$)/.test(value);
    }
    
    /**
    * 检测传入的所有input框中输入的长宽是否是正数
    * @param nodes input框集合，
    */
    function checkNum(nodes) {
        for (var i = 0, ci; ci = nodes[i++]; ) {
            var value = ci.value;
            if (!isNumber(value) && value) {
                ci.value = "";
                ci.focus();
                return false;
            }
        }
        return true;
    }

function convert_url(url) {
        if (!url) return '';
        url = utils.trim(url)
            .replace(/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i, 'player.youku.com/player.php/sid/$1/v.swf')
            .replace(/(www\.)?youtube\.com\/watch\?v=([\w\-]+)/i, "www.youtube.com/v/$2")
            .replace(/youtu.be\/(\w+)$/i, "www.youtube.com/v/$1")
            .replace(/v\.ku6\.com\/.+\/([\w\.]+)\.html.*$/i, "player.ku6.com/refer/$1/v.swf")
            .replace(/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "player.56.com/v_$1.swf")
            .replace(/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "player.56.com/v_$1.swf")
            .replace(/v\.pps\.tv\/play_([\w]+)\.html.*$/i, "player.pps.tv/player/sid/$1/v.swf")
            .replace(/www\.letv\.com\/ptv\/vplay\/([\d]+)\.html.*$/i, "i7.imgs.letv.com/player/swfPlayer.swf?id=$1&autoplay=0")
            .replace(/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i, "www.tudou.com/v/$1")
            .replace(/v\.qq\.com\/cover\/[\w]+\/[\w]+\/([\w]+)\.html/i, "static.video.qq.com/TPout.swf?vid=$1")
            .replace(/v\.qq\.com\/.+[\?\&]vid=([^&]+).*$/i, "static.video.qq.com/TPout.swf?vid=$1")
            .replace(/my\.tv\.sohu\.com\/[\w]+\/[\d]+\/([\d]+)\.shtml.*$/i, "share.vrs.sohu.com/my/v.swf&id=$1");

        return url;
    }

/* 插入上传视频 */
function insertUpload() {
    var uploadVideoList = [];
    var videoObjs = [],
        uploadDir = editor.getOpt('videoUrlPrefix'),
        width = $G('upload_width').value || 420,
        height = $G('upload_height').value || 280,
        align = findFocus("upload_alignment", "name") || 'none';
    var address = editor.getOpt('ftpServerInfo');
    for (var i = videoObjects.length; i > 0; i--) {
        videoObjs.push({
            url: address + "UEditor/Attach/Videos/" + videoObjects[i - 1].url,
            width: width,
            height: height,
            align: align
        });
    }
//        $('.filelist').find('li').each(function() {
//            if ($(this).hasClass('success')) {
//                videoObjs.push({
//                    url: address + "UEditor/Attach/Videos/" + videoObjects[$(this).index()].url,
//                    width: width,
//                    height: height,
//                    align: align
//                });
//            }
//        });
    editor.execCommand('insertvideo', videoObjs, 'upload');
}

function doUpload(id) {
    try {
        var upload_ocx = initUploadOcx();
        var uid = getUid('file');
        var filename = uid + '.' + getFileExt(file_list[id]);
        upload_ocx.doUpload(file_list[id], getFileExt(file_list[id]), uid);
       
        videoObjects.push({
            url: uid + '.' + getFileExt(file_list[id])
        });

        //进度
        var timer = setInterval(function () {
            var iChunk = parseFloat(upload_ocx.Chunk);
            var totalChunks = parseFloat(upload_ocx.Chunks);
            var uploadFlag = upload_ocx.UploadFlag;
            if (uploadFlag == 0) {
                removeFile(path);
                clearInterval(timer);
            } else {
                var percent = parseFloat(iChunk / totalChunks).toFixed(4);
                var objID = 'WU_FILE_' + (id + 1);
                $('#' + objID + ' .progress span').css("width", (percent * 100) + "%");
                if (percent == 1) {
                    $('#' + objID).addClass('success');
                    $('#' + objID + ' .progress span').hide();
                    $('#' + objID + ' .success').show();
                    clearInterval(timer);
                }
            }
        }, 100);
    } catch (e) {
    }
}

function getRootPath_web() {
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPath = curWwwPath.substring(0, pos);
    
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPath + projectName);
}

function getFileExt(path) {
    var tmp = path.split('.');
    return tmp[tmp.length - 1].toLowerCase();
}

function getTotalFileSize() {
    if (file_list.length === 0) {
        return '0';
    }
    var totalFileSize = 0;
    for (var i = file_list.length; i > 0; i--) {
        var fileSize = getLocalFileSize(file_list[i - 1]);
        if (fileSize != -1) {
            totalFileSize += fileSize;  
        }        
    }
    if (totalFileSize === 0) {
        return '0';
    }
    var result = (totalFileSize / (1024 * 1024)).toString().split('.')[0];
    if (result == 0) {
        result = (totalFileSize / (1024 * 1024)).toString().split('.')[0] + '.' + (totalFileSize / (1024 * 1024)).toString().split('.')[1].substring(0, 2);
    }

    return result;
}